<?php


namespace app\common\model;


use think\Db;

class Aftersales extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_after_sales';
    // 主键ID
    protected $pk = 'a_s_id';


    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['a_s_id' => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->a_s_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

//    获取商品售后管理申请列表
    function get_aftersales_list($cond,$field = 'as.*',$orderby = 'as.a_s_id desc',$limit = 10,$join = [],$is_transform = false)
    {
        $where = null;
        if(!empty($cond['user_id'])){
            $where[] = ['as.user_id','=',$cond['user_id']];
        }
        if(!empty($cond['shop_id'])){
            $where[] = ['as.shop_id','=',$cond['shop_id']];
        }
        if(!empty($cond['shop_name'])){
            $where[] = ['s.shop_name','like',"%".$cond['shop_name']."%"];
        }
        $data = $this
            ->alias('as')
            ->join($join)
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->paginate($limit)->each(function($value) use($is_transform){
                if($is_transform){
                    $value['order_product_details'] = json_decode($value['order_product_details'],true);
                }
                $value['images'] = explode(',',$value['images']);
                return $value;
            });
        return $data;
    }

}